package com.surface.hao.component;

import lombok.extern.log4j.Log4j;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;

/**
 * @Description:
 *  树形的菜单信息
 * @Author: songhao
 * @Email:425008064@qq.com
 * @CreateDate: 2019/12/30 17:33
 */
@Log4j
public class TreeMenu {

    private JTree tree = null;

    public TreeMenu() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("工具箱");
        DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("文件");
        DefaultMutableTreeNode child11 = new DefaultMutableTreeNode("文件查询");
        DefaultMutableTreeNode child12 = new DefaultMutableTreeNode("内容查询");
        DefaultMutableTreeNode child13 = new DefaultMutableTreeNode("文件合并");
        DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("日期");
        DefaultMutableTreeNode child31 = new DefaultMutableTreeNode("日期转换");
        //添加第一级菜单
        root.add(child1);
        root.add(child3);

        //添加第二级菜单
        child1.add(child11);
        child1.add(child12);
        child1.add(child13);
        child3.add(child31);

        tree = new JTree(root);
        tree.setPreferredSize(new Dimension(200, 400));
        tree.setFont(new Font("宋体", Font.BOLD, 16));
        tree.addTreeSelectionListener(new TreeSelectionListener() {

            public void valueChanged(TreeSelectionEvent e) {
                log.info("当前点击的菜单是:[" + e.getPath() + "]," + e.getNewLeadSelectionPath().getLastPathComponent());

                String menuName = e.getNewLeadSelectionPath().getLastPathComponent() + "";
                menuChanged(menuName);
            }
        });
    }

    /**
     * 菜单更改
     * @param menuName
     */
    private static void menuChanged(String menuName){
        if(menuName.equals("日期转换")){
        }
    }
}
